package org.adream.account.service;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.adream.account.dao.IntegralHistoryDao;
import org.adream.account.dao.IntegralRuleDao;
import org.adream.account.entity.IntegralRuleEntity;
import org.adream.account.util.IdGenerator;
import org.adream.account.util.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class IntegralRuleService {
	
	@Autowired
	IntegralHistoryDao integralHistoryDao;
	
	@Autowired
	IntegralRuleDao integralRuleDao;
	
	public List<Map<String,Object>> searchIntegralInfo() {
		return integralHistoryDao.searchIntegralInfo();
	}

	/**
	 * 获取积分规则
	 * @return
	 */
	public IntegralRuleEntity getIntegralRule() {
		return integralRuleDao.getIntegralRule();
	}
	
	/**
	 * 保存积分规则
	 * @param entity
	 * @param request
	 * @return
	 */
	public int saveIntegralRule(IntegralRuleEntity entity,HttpServletRequest request) {
		if(StringUtils.isEmpty(entity.getIrContent())) {
			return 0;
		}
		String uid = UserUtil.getUidByRequest(request);
		//删除原来所有有效的积分规则
		integralRuleDao.deleteAll(uid);
		
		entity.setIrId(IdGenerator.getGuid());
		entity.setCreator(uid);
		entity.setModifier(uid);
		return integralRuleDao.insert(entity);
	}
}